<template>
  <div class="app-container">
    <div class="dept-name">{{ deptInfo?.name }}</div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        :stripe="true"
        size="large"
      >
        <el-table-column align="center" label="序号" type="index" width="100" />
        <el-table-column align="center" label="检测人" prop="nickname" />
        <el-table-column align="center" label="性别" prop="gender">
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.gender === 0">未知</el-tag>
            <el-tag type="success" v-if="scope.row.gender === 1">男</el-tag>
            <el-tag type="info" v-if="scope.row.gender === 2">女</el-tag>
          </template>
        </el-table-column>

        <el-table-column align="center" fixed="right" label="操作" width="260">
          <template #default="scope">
            <el-button
              type="success"
              text
              size="small"
              @click.stop="handleFinish(scope.row.id)"
            >
              <i-ep-edit />完成
            </el-button>
            <el-popconfirm
              title="你确定需要删除?"
              @confirm="handleCancel(scope.row.id)"
            >
              <template #reference>
                <el-button type="danger" text size="small">
                  <i-ep-delete />移除
                </el-button>
              </template>
            </el-popconfirm>

            <el-button
              type="primary"
              text
              size="small"
              @click.stop="openDialog(scope.row)"
            >
              <i-ep-edit />插队
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 科室排队人数表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="50%"
      @close="closeDialog"
    >
      <el-scrollbar style="padding: 20px 0" max-height="600px">
        <div class="dept-box">
          <el-card
            class="dept-item"
            v-for="(item, index) in deptQueueUserInfoList"
            :key="index"
          >
            <div class="dept-name">{{ item.deptInfo.name }}</div>
            <div class="userInfo">
              <div
                class="user-item"
                v-for="(user, i) in item.userList"
                :key="user.id"
              >
                <div>{{ i + 1 }}</div>
                <div v-if="currentUser && currentUser.id == user.id">
                  <el-tag type="error">{{ user.nickname }}</el-tag>
                </div>
                <div v-else>{{ user.nickname }}</div>
                <div>
                  <el-button
                    size="small"
                    type="primary"
                    :icon="Plus"
                    circle
                    @click="insertQueue(item.deptInfo, user)"
                  />
                </div>
              </div>
              <div
                style="width: 100%; display: flex; justify-content: center"
                v-if="item.userList?.length == 0"
              >
                <el-button
                  size="small"
                  type="primary"
                  :icon="Plus"
                  circle
                  @click="insertQueue(item.deptInfo, undefined)"
                />
              </div>
            </div>
          </el-card>
        </div>
      </el-scrollbar>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { DeptVO } from "@/api/dept/types";
import {
  deptQueueUserInfoAPI,
  finfishProjectAPI,
  cancelProjectAPI,
  queueDeptORSiblingByDoctorAPI,
  insertQueueAPI,
} from "@/api/queue/index";

import { IUserVo, IDeptVo, IDeptQueueUserInfo } from "@/api/queue/types";
import { UserPageVO } from "@/api/user/types";
import { Plus } from "@element-plus/icons-vue";
import { el } from "element-plus/es/locale";

import { ref, reactive } from "vue";
/* import { getCountryBaseListCheckRecordAPI } from "@/api/countryBase/index";
import { CountryBase } from "@/api/countryBase/types"; */

const total = ref(0);
const loading = ref(false);
const tableData = ref<IUserVo[]>([]);
const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);

const deptQueueUserInfoList = ref<IDeptQueueUserInfo[]>([]);

const deptInfo = ref<IDeptVo>();
const currentUser = ref<IUserVo>();

const dialog = reactive<DialogOption>({
  visible: false,
});

onMounted(() => {
  deptQueueUserInfo();
});

// 获取本科生和并列科室排队情况
const getQueueDeptORSiblingByDoctor = async () => {
  const res = await queueDeptORSiblingByDoctorAPI();
  deptQueueUserInfoList.value = res.data;
};

// 获取科室排队人数列表
function deptQueueUserInfo() {
  loading.value = true;
  deptQueueUserInfoAPI()
    .then(({ data }) => {
      tableData.value = data.userList;
      deptInfo.value = data.deptInfo;
    })
    .finally(() => {
      loading.value = false;
    });
}

const insertQueue = (targetDept: DeptVO, targetUser: UserPageVO) => {
  if (targetUser) {
    ElMessageBox.confirm(
      `是否确定将当前【${currentUser.value?.nickname}】插入到【${targetDept.name}--${targetUser.nickname}】前面`,
      {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }
    )
      .then(() => {
        insertQueueAPI(
          deptInfo.value?.id,
          targetDept.id,
          currentUser.value?.id,
          targetUser.id
        );

        deptQueueUserInfo();
        dialog.visible = false;
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消操作",
        });
      });
  } else {
    ElMessageBox.confirm(
      `是否确定将当前【${currentUser.value?.nickname}】插入到【${targetDept.name}}】里面`,
      {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }
    )
      .then(() => {
        insertQueueAPI(
          deptInfo.value?.id,
          targetDept.id,
          currentUser.value?.id
        );

        deptQueueUserInfo();
        dialog.visible = false;
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消操作",
        });
      });
  }
};

const handleFinish = (userId: number) => {
  if (deptInfo.value?.id) {
    finfishProjectAPI(userId, deptInfo.value.id).then(() => {
      ElMessage.success("操作成功");
      deptQueueUserInfo();
    });
  }
};
const handleCancel = (userId: number) => {
  if (deptInfo.value?.id) {
    cancelProjectAPI(userId, deptInfo.value.id).then(() => {
      ElMessage.success("操作成功");
      deptQueueUserInfo();
    });
  }
};
/** 关闭表单弹窗 */
function closeDialog() {
  dialog.visible = false;
}

/** 打开科室排队人数表单弹窗 */
function openDialog(current: IUserVo) {
  currentUser.value = current;
  getQueueDeptORSiblingByDoctor();
  dialog.visible = true;
}
</script>

<style lang="scss" scoped>
.dept-name {
  width: 100%;
  text-align: center;
  height: 60px;
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 5px;
  line-height: 60px;
}
.dept-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-content: center;
  .dept-item {
    .dept-name {
      color: rgb(85, 120, 224);
    }
    margin-top: 10px;
    margin-left: 20px;
    display: flex;
    flex-direction: column;
    align-content: center;
    width: 400px;

    .userInfo {
      width: 100%;
      padding: 0 60px;
      .user-item {
        display: flex;
        margin-top: 10px;
        justify-content: space-between;
      }
    }
  }
  .dept-item:hover {
    transform: scale(1.02);
  }
}
</style>
